﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using BLL;
using DAL;
using NLog;

namespace QLTTTH
{
    public partial class frmThongTinKyThi : DevExpress.XtraEditors.XtraForm
    {

        private Logger logger = LogManager.GetCurrentClassLogger();

        public frmThongTinKyThi()
        {
            InitializeComponent();
        }

        public void FillControl(KyThi kt)
        {
            this.txtMaKT.Text = kt.MaKyThi.ToString();
            this.loupKhoaHoc.EditValue = kt.MaKH;
            this.dtpNgayThi.EditValue = kt.NgayThi;
            this.txtHinhThuc.Text = kt.HinhThucThi;
            this.txtThoiGian.Text = kt.ThoiGianThi;
        }

        private void frmThongTinKyThi_Load(object sender, EventArgs e)
        {
            var aResult = KhoaHocManager.LayDanhSachKhoaHoc();
            if (aResult.Success)
            {
                loupKhoaHoc.Properties.DataSource = aResult.Result;
                loupKhoaHoc.Properties.ValueMember = "MaKH";
                loupKhoaHoc.Properties.DisplayMember = "TenKH";
                if (loupKhoaHoc.ItemIndex < 0)
                    loupKhoaHoc.ItemIndex = 0;
                if (string.IsNullOrEmpty(dtpNgayThi.Text.ToString()))
                    dtpNgayThi.EditValue = DateTime.Now;
            }
        }

        public delegate void SendData(KyThi kt);

        public SendData sendData;

        private void btnLuu_Click(object sender, EventArgs e)
        {
            if (sendData != null)
            {
                var vResult = new KyThiManager().Validate(GetKyThi());
                if (vResult.Valid)
                {
                    sendData(GetKyThi());
                    DialogResult = DialogResult.OK;
                    this.Close();
                }
                else
                {
                    logger.Info(vResult.ToErrorString());
                    XtraMessageBox.Show(vResult.ToString(), "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private KyThi GetKyThi()
        {
            var kt = new KyThi();

            if (!txtMaKT.Text.ToString().Equals(""))
            {
                kt.MaKyThi = int.Parse(txtMaKT.Text.ToString());
            }
            kt.MaKH = int.Parse(loupKhoaHoc.EditValue.ToString());
            kt.NgayThi = DateTime.Parse(dtpNgayThi.Text.ToString());
            kt.HinhThucThi = txtHinhThuc.Text;
            kt.ThoiGianThi = txtThoiGian.Text;

            return kt;
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

    }
}